package com.dbz.controller;

import com.dbz.dao.RoleDao;
import com.dbz.entity.Modules;
import com.dbz.entity.Roles;
import com.dbz.entity.RolesMopdule;
import com.dbz.service.ModulesService;
import com.dbz.service.RoleService;
import com.dbz.service.RolesModulesService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/roles/")
public class RolesController {
    @Resource
    private RoleService roleService;
    @Resource
    private ModulesService modulesService;
    @Resource
    private RolesModulesService rolesModulesService;

    /**
     * 条件分页查询
     *
     * @param rname
     * @param page
     * @param rows
     * @return
     */
    @ResponseBody
    @RequestMapping("selectRoles")
    public Map<String, Object> selectRoles(String rname, Integer page,
                                           Integer rows) {
        Map<String, Object> map = new HashMap<String, Object>();
        Integer pages = (page - 1) * rows;
        map.put("rows", roleService.selectRoles(rname, pages, rows));
        map.put("total", roleService.selectRoles(rname, null, null).size());
        return map;
    }

    /**
     * 添加角色信息
     *
     * @param rname
     * @return
     */
    @ResponseBody
    @RequestMapping("insertRoles")
    public Map<String, Object> insertRoles(String rname) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Roles> rolesList = roleService.getAll();
        System.out.println(rolesList);
        System.out.println(rname);
        for (Roles roles : rolesList) {
            System.out.println(roles.getRname());
            System.out.println(roles.getRname().equals(rname));
            if (roles.getRname().equals(rname)) {
                map.put("no", "添加重复名字");
                return map;
            }
        }
        map.put("ok", roleService.insertRoles(rname));
        return map;
    }

    /**
     * 删除角色信息
     *
     * @param rid
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Map<String, Object> deleteById(String rid) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<RolesMopdule> rolesMopduleList = rolesModulesService.selectRoMoByRid(rid);
        if (rolesMopduleList.size()>0) {
            map.put("no", "角色拥有模块信息不能删除");
            return map;
        }
        map.put("ok", roleService.deleteById(rid));
        return map;
    }

    /**
     * 修改角色信息
     *
     * @param rid
     * @return
     */
    @ResponseBody
    @RequestMapping("updateById")
    public Map<String, Object> updateById(String rid, String rname) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Roles> rolesList = roleService.getAll();
        Roles role = roleService.selectRolesById(rid);
        for (Roles roles : rolesList) {
            if (roles.getRname().equals(rname)) {
                if (role.getRname().equals(rname)) {
                    map.put("ok", roleService.updateById(rid, rname));
                    return map;
                }
                map.put("no", "修改重复名字");
                return map;
            }
        }
        map.put("ok", roleService.updateById(rid, rname));
        return map;
    }


    /**
     * 无条件查询所有
     *
     * @return
     */
    @RequestMapping("getAll")
    public List getAll() {
        return roleService.getAll();
    }

    /*根据用户id查询角色*/

    /*  @ResponseBody
    @RequestMapping("getAllByUserId")
    public List getAllByUserId(String uid) {
        return roleService.getAll(uid);
    }*/

    /**
     * 根据用户id查询角色
     *
     * @param uid
     * @return
     */
    @RequestMapping("getAllByUserId")
    public List getAllByUserId1(String uid) {
        return roleService.getAll(uid);
    }

    /**
     * 根据角色id查询角色拥有模块
     *
     * @param rid
     * @return
     */
    @ResponseBody
    @RequestMapping("selectRoleModules")
    public List<Modules> selectRoleModules(String rid) {
        return roleService.selectRoleModules(rid);
    }

    /**
     * 添加角色模块中间表信息
     *
     * @param rid
     * @param mids
     * @return
     */
    @ResponseBody
    @RequestMapping("insertRoleAndModule")
    public Map<String, Object> insertRoleAndModule(String rid, String mids) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Modules> rolesList = roleService.selectRoleModules(rid);
        for (Modules modules : rolesList) {
            if (modules.getMids().equals(mids)) {
                map.put("no", "添加权限重复");
                return map;
            }
        }
        map.put("ok", rolesModulesService.insertRoleAndModule(rid, mids));
        return map;

    }

    /**
     * 移除模块
     *
     * @param rid
     * @param mids
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteRoleAndModules")
    public int deleteRoleAndModules(String rid, String mids) {
        return rolesModulesService.deleteRoleAndModules(rid, mids);
    }
}
